package com.zcc.blog.controller;


import com.zcc.blog.entity.Category;
import com.zcc.blog.service.CategoryService;
import com.zcc.blog.untils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zcc
 * @since 2022-09-11
 */
@Api(tags = "分类")
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    CategoryService categoryService;

    @ApiOperation("分页查找分类")
    @GetMapping("/categories")
    public Result findCategories(@RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "5") Integer size, Long id){
        return categoryService.findCategories(currentPage, size, id, 0);
    }

    @ApiOperation("查找所有的分类")
    @GetMapping("/getAll/{id}")
    public Result getAllCategories(@PathVariable Long id){
        return categoryService.getAllCategories(id, 0);
    }

    @ApiOperation("通过分类id分页查找博客")
    @GetMapping("/getCategory")
    public Result getCategoryById(@RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "5") Integer size, Long id){
        return categoryService.getBlogsByCategoryId(currentPage, size, id);
    }

    @ApiOperation("搜索分类")
    @GetMapping("/search")
    public Result searchCategories(@RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "5") Integer size, String name, Long id){
        return categoryService.searchCategories(name, currentPage, size, id, 0);
    }

    @ApiOperation("新增分类")
    @RequiresAuthentication
    @PostMapping("/add")
    public Result addCategory(@RequestBody Category category){
        return categoryService.addCategory(category);
    }

    @ApiOperation("删除分类")
    @RequiresAuthentication
    @PutMapping("/delete/{id}")
    public Result deleteCategory(@PathVariable Long id){
        return categoryService.deleteCategory(id);
    }

    @ApiOperation("修改分类")
    @RequiresAuthentication
    @PutMapping("/update")
    public Result updateCategory(@RequestBody Category category){
        return categoryService.updateCategory(category);
    }

    @ApiOperation("统计用户的所有分类数量")
    @GetMapping("/countCategories/{id}")
    public Result countCategories(@PathVariable Long id) {
        return categoryService.countCategories(id);
    }
}
